package com.demo.kit;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class StrKit {

	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'A' && firstChar <= 'Z') {
			char[] arr = str.toCharArray();
			arr[0] += ('a' - 'A');
			return new String(arr);
		}
		return str;
	}

	/**
	 * 首字母变大写
	 */
	public static String firstCharToUpperCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'a' && firstChar <= 'z') {
			char[] arr = str.toCharArray();
			arr[0] -= ('a' - 'A');
			return new String(arr);
		}
		return str;
	}

	/**
	 * 字符串为 null 或者内部字符全部为 ' ' '\t' '\n' '\r' 这四类字符时返回 true
	 */
	public static boolean isBlank(String str) {
		if (str == null) {
			return true;
		}
		int len = str.length();
		if (len == 0) {
			return true;
		}
		for (int i = 0; i < len; i++) {
			switch (str.charAt(i)) {
			case ' ':
			case '\t':
			case '\n':
			case '\r':
				// case '\b':
				// case '\f':
				break;
			default:
				return false;
			}
		}
		return true;
	}

	public static boolean notBlank(String str) {
		return !isBlank(str);
	}

	public static boolean notBlank(String... strings) {
		if (strings == null) {
			return false;
		}
		for (String str : strings) {
			if (isBlank(str)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 检查数组元素全部不为null
	 * 
	 * @param paras
	 * @return
	 */
	public static boolean notNull(Object... paras) {
		if (paras == null) {
			return false;
		}
		for (Object obj : paras) {
			if (obj == null) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 根据字符串中下划线的位置,转换为驼峰
	 * 
	 * @param stringWithUnderline
	 *            eg:_abcD
	 * @return eg:Abcd
	 */
	public static String toCamelCase(String stringWithUnderline) {
		if (stringWithUnderline.indexOf('_') == -1) {
			return stringWithUnderline;
		}

		stringWithUnderline = stringWithUnderline.toLowerCase();
		char[] fromArray = stringWithUnderline.toCharArray();
		char[] toArray = new char[fromArray.length];
		int j = 0;
		for (int i = 0; i < fromArray.length; i++) {
			if (fromArray[i] == '_') {
				// 当前字符为下划线时，将指针后移一位，将紧随下划线后面一个字符转成大写并存放
				i++;
				if (i < fromArray.length) {
					toArray[j++] = Character.toUpperCase(fromArray[i]);
				}
			} else {
				toArray[j++] = fromArray[i];
			}
		}
		return new String(toArray, 0, j);
	}

	public static String join(String[] stringArray) {
		StringBuilder sb = new StringBuilder();
		for (String s : stringArray) {
			sb.append(s);
		}
		return sb.toString();
	}

	/**
	 * 根据指定的连接符，将字符串数组拼接
	 * 
	 * @param stringArray
	 *            eg:{a,b,c}
	 * @param separator
	 *            eg:"_"
	 * @return a_b_c
	 */
	public static String join(String[] stringArray, String separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < stringArray.length; i++) {
			if (i > 0) {
				sb.append(separator);
			}
			sb.append(stringArray[i]);
		}
		return sb.toString();
	}

	public static String getRandomUUID() {
		return java.util.UUID.randomUUID().toString().replace("-", "");
	}

	/**
	 * 格式化数字，不足8位前面补零
	 * 
	 * @param num
	 *            如 1
	 * @return 00000001
	 */
	public static String formatInt(int num) {
		return String.format("%8d", num).replace(" ", "0");
	}

	/**
	 * 格式化数字,数字前面补0
	 * Example: 
	 * public static String formatInt(8, 4){}  return 0008 
	 * public static String formatInt(8, 6){}  return 000008 
	 */
	public static String formatInt(int num, int place) {
		return String.format("%0" + place + "d", num);
	}

	/**
	 * 输入八位字符串，切割成三部分作为目录路径
	 * 
	 * @param str
	 *            如传入 00000001
	 * @return firstDir：00 firstDir：00 thirdDir：0001
	 */
	public static Map<String, String> getDir(String str) {
		Map<String, String> dirPathMap = new HashMap<String, String>();
		dirPathMap.put("firstDir", str.split(str.substring(2))[0]);
		String str1 = str.substring(2);
		dirPathMap.put("secondDir", str1.split(str1.substring(2))[0]);
		dirPathMap.put("thirdDir", str.substring(4, 8));
		return dirPathMap;
	}

	/**
	 * 当前时间字符串
	 * 
	 * @param full
	 *            是否精确到秒
	 * @return
	 */
	public static String getTime(Boolean full) {
		Date date = new Date();
		String format = full ? "yyyy-MM-dd HH:mm:ss" : "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String time = sdf.format(date);
		return time;
	}

	public static String getTime() {
		return getTime(false);
	}

	/**
	 * 获得时间戳的字符串
	 */
	public static String getTimetemp() {
		return String.valueOf(System.currentTimeMillis());
	}
	
	/**
	 * 对路径字符串的修改，如： /upload/2016-11-18/1234567.jpg转为\\upload\\2016-11-18\\200_1234567.jpg
	 * @param filePath	需要修改的路径
	 * @param replaceStr	代替的字符串
	 * @return
	 */
	public static String getImgPath(String filePath , String replaceStr){
		StringBuilder builder = new StringBuilder(filePath);
		builder.replace(filePath.lastIndexOf("/"), filePath.lastIndexOf("/")+1, replaceStr);
		return builder.toString().replace("/", "\\");
	}
}
